#include "widget.h"
#include "ui_widget.h"

#include <QtGui>
#include <QtNetwork>
#include "nam.h"
#include "nbr.h"
//#include "t_networkaccesshttpbackend.h"
#include "t_my_httpbackend.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

	QString v_desktopPath =
			QDesktopServices::storageLocation(
					QDesktopServices::DesktopLocation);
	QDir dir(v_desktopPath);
	QString v_cacheDirPath = dir.filePath("engine2.cache");
	qDebug() << "[v_cacheDirPath]" << v_cacheDirPath;
	m_nc = new QNetworkDiskCache(this);
	m_nc->setCacheDirectory(v_cacheDirPath);


	QUrl v_url("http://www.google.com/");
	QNetworkCacheMetaData m_metaData;
	m_metaData.setUrl(v_url);
	m_metaData.setSaveToDisk(true);
	QIODevice *v_dev = m_nc->prepare(m_metaData);
	qDebug() << "[v_dev]" << v_dev;
	QTemporaryFile *v_tf = dynamic_cast<QTemporaryFile *>(v_dev);
	if(v_tf)
	{
		qDebug() << v_tf->fileName();
	}
	qDebug() << v_dev->isSequential();
	qDebug() << v_dev->size();
	v_dev->write("abcxyz:");
	v_dev->write(QLocale::c().toString(QDateTime::currentDateTime(), QLocale::LongFormat).toUtf8());
	qDebug() << v_dev->size();
	m_nc->insert(v_dev);
	//delete v_dev;
	QIODevice *v_data_1 = m_nc->data(v_url);
	qDebug() << v_data_1->size();
	qDebug() << v_data_1->readAll();
	//delete v_data_1;
	QIODevice *v_data_2 = m_nc->data(v_url);
	v_data_2->write("<append>");
	qDebug() << v_data_2->size();
	//delete v_data_2;
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_clicked()
{
	m_proxy.setType(QNetworkProxy::HttpProxy);
	//m_proxy.setType(QNetworkProxy::HttpCachingProxy);
	m_proxy.setHostName("localhost");
	m_proxy.setPort(8080);
	if (m_proxy.type() != QNetworkProxy::HttpProxy)
	{
		qDebug() << "ng!";
	}
	else
	{
		qDebug() << "ok!";
	}
	NetworkAccessManager * nam2 = new NetworkAccessManager;
	////nam2->setProxy(m_proxy);
	QPointer<T_MyHttpBackend> v_be = new T_MyHttpBackend;
	v_be = 0;
	qDebug() << "[v_be(1)]" << v_be;
	QUrl v_url("http://www.google.co.jp");
	//QUrl v_url("https://www3.idpass-net.nenkin.go.jp/neko/action/z0401");
	//QUrl v_url("https://www3.idpass-net.nenkin.go.jp/neko/action/verify");

	// http://www.tohoho-web.com/ex/http.htm#Range
	QNetworkRequest v_request(v_url);
	v_request.setRawHeader("Range", "bytes=0-20");
	QNetworkReply *v_reply = nam2->createRequest(QNetworkAccessManager::GetOperation, v_request, 0, v_be);
	QByteArray v_response;
	NetworkBatchRequest v_nbr(v_reply, &v_response, false);
	v_nbr.wait();
	qDebug() << v_response;
	if(v_reply) delete v_reply;
	qDebug() << "[v_be(2)]" << v_be;
}


void Widget::on_pushButton_2_clicked()
{
	NetworkAccessManager * nam2 = new NetworkAccessManager;
	//QUrl v_url("http://www.google.co.jp");
	QUrl v_url("http://v13.lscache2.c.youtube.com/videoplayback?ip=110.0.0.0&sparams=id,expire,ip,ipbits,itag,algorithm,burst,factor&fexp=902300,907042&algorithm=throttle-factor&itag=34&ipbits=8&burst=40&sver=3&expire=1277974800&key=yt1&signature=B993199C3DF1E7FA3E51CCD58DBFBAF5D2C3C7D1.265062AB0430065B4F1BBE6E0BFBDF67A8861745&factor=1.25&id=83bff93c1af58b80&");

	QNetworkRequest v_request(v_url);
	//v_request.setRawHeader("Range", "bytes=0-20");
	v_request.setRawHeader("Range", "bytes=0-30");
	QNetworkReply *v_reply = nam2->createRequest(QNetworkAccessManager::GetOperation, v_request, 0);
	QByteArray v_response;
	NetworkBatchRequest v_nbr(v_reply, &v_response, true);
	v_nbr.wait();
	qDebug() << v_response;
	////qDebug() << v_reply->rawHeader("Content-Length");
	QList<QByteArray> v_headerList = v_reply->rawHeaderList();
	foreach(QByteArray v_first, v_headerList)
	{
		QByteArray v_second = v_reply->rawHeader(v_first);
		qDebug() << "Widget::on_pushButton_2_clicked():" << v_first << v_second;
	}
	if(v_reply) delete v_reply;
}
